package com.xt.purchasingsystem.service.impl.stockManagementServiceImpl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xt.purchasingsystem.dto.DFileDto;
import com.xt.purchasingsystem.dto.SGatherDto;
import com.xt.purchasingsystem.mapper.DFileMapper;
import com.xt.purchasingsystem.mapper.SGatherDetailsMapper;
import com.xt.purchasingsystem.mapper.SGatherMapper;
import com.xt.purchasingsystem.model.DFile;
import com.xt.purchasingsystem.model.SGather;
import com.xt.purchasingsystem.model.SGatherDetails;
import com.xt.purchasingsystem.service.stockManagementService.WarehousingApplicationManageService;
import com.xt.purchasingsystem.utils.Result;
import com.xt.purchasingsystem.utils.ResultFactory;
import com.xt.purchasingsystem.utils.SerialNumberUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author: 老姚
 * Date:  2020/5/25 21:24
 */
@Service
@Transactional
public class WarehousingApplicationManageServiceImpl implements WarehousingApplicationManageService {
    @Autowired
    private DFileMapper dFileMapper;
    @Autowired
    private SGatherMapper sGatherMapper;
    @Autowired
    private SGatherDetailsMapper sGatherDetailsMapper;

    @Override
    public Result getAllWarehousingDfileByDto(DFileDto dFileDto) {
        PageHelper.startPage(dFileDto.getCurrNo(), dFileDto.getPageSize());
        List<DFile> allWarehousingDfileByDto = dFileMapper.getAllWarehousingDfileByDto(dFileDto);
        PageInfo<DFile> pageInfo = new PageInfo<>(allWarehousingDfileByDto);
        return ResultFactory.buildSuccessResult("查询成功", pageInfo);
    }

    @Override
    public Result submitWarehouse(SGatherDto sGatherDto) {
        sGatherDto.getSgather().setGatherId(SerialNumberUtil.godownEntrySerNumber());
        sGatherMapper.insert(sGatherDto.getSgather());
        Integer id = sGatherDto.getSgather().getId();
        for (SGatherDetails sgatherDetail : sGatherDto.getSgatherDetails()) {
            sgatherDetail.setParentId(id);
            sGatherDetailsMapper.insert(sgatherDetail);
        }
        return ResultFactory.buildSuccessResult("提交成功,需要审核");
    }

    @Override
    public Result getAllNoCheckSgatherByPage(Integer currNo, Integer pageSize) {
        PageHelper.startPage(currNo, pageSize);
        List<SGatherDto> allNoCheckSgather = sGatherMapper.getAllNoCheckSgather();
        PageInfo<SGatherDto> pageInfo = new PageInfo<>(allNoCheckSgather);
        return ResultFactory.buildSuccessResult("提交成功", pageInfo);
    }

    @Override
    public Result checkSgather(SGather sGather) {
        int i = sGatherMapper.updateByPrimaryKey(sGather);
        if (i>0){
            return ResultFactory.buildSuccessResult("审核成功");
        }else {
            return ResultFactory.buildFailResult("审核失败");
        }
    }
}
